<?php
/**
 * Output
 *
 * @category	Log
 * @package		dayscript.log.adapter
 * @subpackage
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-02 $
 */

namespace dayscript\log\adapter;

/**
 * Store
 *
 * @category	Log
 * @package		dayscript.log.adapter
 * @subpackage
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\Map, \dayscript\log\Logger
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-03 $
 */
use dayscript\log\LogManager;

use dayscript;

class Output extends \dayscript\log\Logger	{
	/**
	 * Whether use html output.
	 * @var boolean
	 */
	protected $html = false;
	/**
	 * HTML replacements for a good looking debug.
	 * @var array
	 */
	protected $htmlReserved = array(
		']  APP ',
		']  PHP ',
		'APPLICATION INIT.',
		'APPLICATION END.',
		'APPLICATION ',
		'</font> SESSION ',
		'</font> SHUTDOWN ',
		'EVENT STOP ',
		'EVENT ',
		'USER'
	);
	protected $htmlReplacement = array(
		'] <font color="#5E9C56"><b>APP</b></font> ',
		'] <font color="#5E569C"><b>PHP</b></font> ',
		'<font color="#3D5939"><b>APPLICATION INIT</b></font>.',
		'<font color="#3D5939"><b>APPLICATION END</b></font>.',
		'<font color="#3D5939"><b>APPLICATION</b></font> ',
		'</font> <font color="#6666FF"><b>SESSION</b></font> ',
		'</font> <font color="#000000"><b>SHUTDOWN</b></font> ',
		'<font color="#97A14F"><b>EVENT</b></font> <font color="#5E6331"><b>STOP</b></font> ',
		'<font color="#97A14F"><b>EVENT</b></font> ',
		'<font color="#3D5939"><b>USER</b></font>'
	);
	/**
	 * Output constructor
	 * @param string $name Output identificator
	 * @param array|\dayscript\Map $config File configuration
	 * @return Logger
	 */
	public function __construct( $name, \dayscript\Map $config )	{
		parent::__construct( $name, $config );
		foreach( \dayscript\ErrorManager::$levels as $text )	{
			\array_unshift( $this->htmlReserved, $text );
			\array_unshift( $this->htmlReplacement, '<font color="#C22B2B"><b>' . $text . '</b></font>' );
		}
		\array_unshift( $this->htmlReserved, 'FATAL TERMINAL ERROR' );
		\array_unshift( $this->htmlReplacement, '<font color="#F00"><b>FATAL TERMINAL ERROR</b></font>' );
	}
	/**
	 * Output can log in a Web page o console application, when in a Web page it needs that the headers has already been sent.
	 * @see \dayscript\log\Logger
	 */
	public function flush( )	{
		if( count( $this->buffer ) > 0 )	{
			
			if( $this->useBuffer )	{
				$this->html = ( $this->html || \dayscript\Enviroment::server( 'SERVER_NAME' ) != null );
				$this->useBuffer = ( ( $this->html && !\headers_sent( ) ) || \dayscript\log\LogManager::getType( ) != \dayscript\log\LogManager::TYPE_DIRECT );
			}
			
			if( !$this->useBuffer )	{
				echo "\n" . '<div style="clear:both">' . "\n";
				$isHTML = ( $this->html && \headers_sent( $file, $line ) && $file != __FILE__ );
				foreach( $this->buffer as $msg )	{
					$msg = \html_entity_decode( \str_replace( "\r", "", \rawurldecode( $msg ) ) );
					$file = $line = null;
					if( $isHTML )	{
						$msg = \nl2br( \str_replace( $this->htmlReserved, $this->htmlReplacement, \htmlentities ( $msg ) ) );
						// . ' : [' . $file . ':' . $line . ']';
					}
					else
						$msg = \strip_tags( $msg );
					echo $msg;
				}
				echo "\n" . '</div>' . "\n";
				$this->buffer = array( );
			}
		}
	}
	
}